const url = window.g_ctx.getServiceByName('HOME_SVR');

/**
 * 查询政策法规列表
 * @param {*} params
 * @returns
 */
export function getAttachList(params) {
  const { page = 1, pageSize = 5, ...restParams } = params;
  if (!restParams.title) {
    delete restParams.title;
  }
  return request({
    type: 'post',
    data: {
      pageNo: page,
      pageSize,
      param: JSON.stringify(restParams),
    },
    url: `${url}/policy/listBriefPolicy`,
  });
}

/**
 * 通过类型统计数量
 * @returns {Promise<any>}
 */
export function getAttachTypeData() {
  return request({
    url: `${url}/policy/countType`,
  });
}

// 政策法规详情
export function queryInfoDetali(params) {
  const { id } = params;
  return request({
    type: 'post',
    url: `${url}/policy/getPolicyDetail`,
    data: {
      param: JSON.stringify({ id }),
    },
  });
}

// 附件下载
export function getDownLoadAttachment(ids) {
  return request({
    type: 'get',
    url: `${url}/file/packageDownload?ids=${ids}`,
  });
}
export function getDownUrl(ids) {
  return `${url}/file/packageDownload?ids=${ids}`;
}

/**
 * @param {Object} options
 * @param {string} [options.type=get] - Http Request Method
 * @param {string} options.url - Request URL
 * @param {any} options.data - Request Body
 * @returns
 */
function request(options) {
  const { type = 'get', url, data = {} } = options;
  return new Promise((resolve, reject) => {
    $.ajax({
      type,
      url,
      data,
      success(res) {
        resolve(res);
      },
      error(xhr) {
        reject(new Error(xhr.statusText));
      },
    });
  });
}
